<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Ajax with token</title>
  </head>
  <body>
    <script>
      // 前置增强函数
      Function.prototype.before = function (beforeFunction) {
        const originalFunction = this;
        return function () {
          beforeFunction(...arguments);
          return originalFunction(...arguments);
        };
      };

      function ajax(url, method, params) {
        console.log(url, method, params);
      }

      // 带上 token 字符串的 ajax
      const ajaxWithToken = ajax.before((url, method, params) => {
        params.token = "eeeeeeeeeeeeeeeeeeeeeee";
      });

      ajax("/api", "post", {
        name: "Lucy",
      });

      ajaxWithToken("/api", "post", {
        name: "Lucy",
      });
    </script>
  </body>
</html>
